// import { from } from "core-js/core/array";
import { createRouter, createWebHistory } from "vue-router";
import { routes } from "./routes";
import store from "@/store";
import { getToken } from "@/request/token";
import { ElMessage } from "element-plus";
import "element-plus/es/components/message/style/css";
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});

router.beforeEach((to, from, next) => {
  if (getToken()) {
    if (to.path === "/MyLogin") {
      next({ path: "/" });
    } else {
      if (store.state.account.length === 0) {
        store
          .dispatch("getUserInfo")
          .then((data) => {
            //获取用户信息
            next();
          })
          .catch(() => {
            next({ path: "/" });
          });
      } else {
        next();
      }
    }
  } else {
    if (to.matched.some((r) => r.meta.requireLogin)) {
      ElMessage({
        type: "warning",
        showClose: true,
        message: "请先登录哦",
      });
    } else {
      next();
    }
  }
});

export default router;
